package view;


import controller.MainListener;
import controller.SelfManageClickListener;
import controller.SelfManageListener;
import pojo.Book;
import pojo.Manage;
import service.BookService;
import service.ManageService;

import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
import java.util.List;

public class SelfManageView extends JPanel {
    private String userId;
    private List<Manage> manageList;
    private JScrollPane scroll;
    private MainListener listener;
    private MainJFrame mainJFrame;
    private SelfManageClickListener selfManageClickListener;
    private SelfManageListener selfManageListener;
    private JLabel jLabel1,jLabel2,jLabel3,jLabel4,labelSelf;

    public SelfManageView(List<Manage> manageList, String  userId) {
        selfManageClickListener =new SelfManageClickListener(this);
        selfManageListener =new SelfManageListener(this,userId);
        this.userId = userId;
        this.manageList = manageList;
        this.setLayout(new BorderLayout());
        this.add(this.getLabel(),BorderLayout.NORTH);
        this.add(this.getPanel(),BorderLayout.CENTER);
        this.add(this.getButton(),BorderLayout.SOUTH);
    }

    //刷新
    public void refresh(){
        ManageService service = new ManageService();
        manageList = service.searchAll();
        this.remove(scroll);
        this.add(this.getPanel());
        //刷新
        this.validate();
    }

    public JPanel getLabel(){
        JPanel panel=new JPanel();
        labelSelf=new JLabel("已借图书");
        labelSelf.setFont(( new Font( "隶书" , Font.BOLD , 30)));
        panel.add(labelSelf);
        return panel;
    }
    public JScrollPane getPanel() {
        String[] titles = { "书号","书名", "租借日期", "归还日期"};
        int size=0;
        for (Manage manage : manageList) {
            if(manage.getUserId().equals(userId)){
                size++;
            }
        }

        Object data[][] = new Object[size][4];
        Iterator<Manage> iter = manageList.iterator();
        int i=0;
        for (Manage manage : manageList) {
            if(manage.getUserId().equals(userId)){
                data[i][0] = manage.getBookId();
                data[i][1] = manage.getBookName();
                data[i][2] = manage.getLendTime();
                data[i][3] = manage.getReturnTime();
                i++;
            }
        }
        JTable table = new JTable(data, titles);
        table.addMouseListener(selfManageClickListener);
        selfManageClickListener.setTable(table);
        scroll = new JScrollPane(table);
        return scroll;
        }
    public JPanel getButton(){
        JPanel jPanel=new JPanel();
        jPanel.setLayout(new GridLayout(2,1));
        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        jLabel3=new JLabel();
        jLabel4=new JLabel();
        jLabel1 = new JLabel("书号: ");
        jLabel2 = new JLabel("书名: ");
        JButton jButton=new JButton("续借");
        JButton jButton1=new JButton("归还");

        jButton.addActionListener(selfManageListener);
        jButton1.addActionListener(selfManageListener);

        jPanel1.add(jLabel1);
        jPanel1.add(jLabel3);
        jPanel1.add(jLabel2);
        jPanel1.add(jLabel4);
        jPanel2.add(jButton);
        jPanel2.add(jButton1);
        jPanel.add(jPanel1);
        jPanel.add(jPanel2);
        return jPanel;
    }

    public String getUserId() {
        return userId;
    }

    public JLabel getJLabel1() {
        return jLabel1;
    }

    public void setJLabel1(JLabel jLabel1) {
        this.jLabel1 = jLabel1;
    }

    public JLabel getJLabel2() {
        return jLabel2;
    }

    public void setJLabel2(JLabel jLabel2) {
        this.jLabel2 = jLabel2;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public JLabel getJLabel3() {
        return jLabel3;
    }

    public void setJLabel3(JLabel jLabel3) {
        this.jLabel3 = jLabel3;
    }

    public JLabel getJLabel4() {
        return jLabel4;
    }

    public void setJLabel4(JLabel jLabel4) {
        this.jLabel4 = jLabel4;
    }
}



